package com.fuyd.pms.background.web.merchant;

import com.fuyd.pms.background.service.account.AccountService;
import com.fuyd.pms.background.web.base.BaseController;
import com.fuyd.pms.entity.Merchant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 商户信息
 * Detail page      : GET   /merchant/detail
 * Update page      : GET   /merchant/update
 * Update action    : POST  /merchant/update
 * Created by fuyongde on 2015/12/29.
 */
@Controller
@RequestMapping(value = "merchant")
public class MerchantController extends BaseController {

    private AccountService accountService;

    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    /**
     * 商户
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(Model model){
        Merchant merchant = getCurrentUser().getMerchant();
        model.addAttribute("merchant", merchant);
        return "merchant/detail";
    }

    /**
     * 修改商户信息页面
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Model model) {
        Merchant merchant = getCurrentUser().getMerchant();
        model.addAttribute("merchant", merchant);
        return "merchant/update";
    }

    /**
     * 修改商户信息
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String doUpdate() {
        // TODO: 2015/12/29 修改商户action
        return "redirect:/merchant/detail";
    }

    /**
     * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
     * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
     */
    @ModelAttribute
    public void getTask(@RequestParam(value = "id", defaultValue = "") String id, Model model) {
        if (StringUtils.isNotEmpty(id)) {
            model.addAttribute("merchant", accountService.findById(id));
        }
    }
}
